package com.orm.analysis.action;

import com.opensymphony.xwork2.ActionContext;
import com.orm.analysis.model.Item;
import com.orm.analysis.model.OrderDetail;
import com.orm.analysis.service.GenericService;

import java.util.*;


public class OrderDetailAction extends FrontendAction {

    private GenericService<OrderDetail> service;
    private GenericService<Item> itemService;
    private Map<Integer, OrderDetail> orderDetailMap = new HashMap<Integer, OrderDetail>();
    private Collection<OrderDetail> orderDetails = new ArrayList<OrderDetail>();
    private OrderDetail orderDetail;


    public OrderDetailAction(GenericService<OrderDetail> service, GenericService<Item> itemService) {
        this.service = service;
        this.itemService = itemService;
    }

    public String getAllOrderDetails() {
        prep();
        retrievingOrdersDetails();
        return SUCCESS;
    }

    public String list() {
        prep();
        retrievingOrdersDetails();
        return SUCCESS;
    }

    public String setUpForInsert() {
        prep();
        retrievingOrdersDetails();

        return "success";
    }
  public String setUpForUpdate() {
        prep();
        retrievingOrdersDetails();
        if (orderDetail != null && orderDetail.getItem() != null && orderDetail.getItem().getItemId() != null) {

            orderDetail = orderDetailMap.get(orderDetail.getItem().getItemId());

        }
        return "success";
    }


    public String insert() {

        prep();
        if (!validationSuccessful()) {
            return "input";
        } else {
            if (orderDetail.getItem().getItemId() != null) {
                retrievingOrdersDetails();
                if (orderDetailMap.containsKey(orderDetail.getItem().getItemId())) {
                    orderDetailMap.remove(orderDetail.getItem().getItemId());
                }

                orderDetail.setItem(itemService.getById(orderDetail.getItem().getItemId()));
                orderDetail.setLineAmount(orderDetail.getItem().getPrice() * orderDetail.getQuantity());

                orderDetailMap.put(orderDetail.getItem().getItemId(), orderDetail);

                storingOrdersDetails();
            }

        }
        return "success";
    }

 public String update() {

        prep();
        if (!validationSuccessful()) {
            return "input";
        } else {
            if (orderDetail.getItem().getItemId() != null) {
                retrievingOrdersDetails();
                if (orderDetailMap.containsKey(orderDetail.getItem().getItemId())) {
                    orderDetailMap.remove(orderDetail.getItem().getItemId());
                }

                orderDetail.setItem(itemService.getById(orderDetail.getItem().getItemId()));
                orderDetail.setLineAmount(orderDetail.getItem().getPrice() * orderDetail.getQuantity());

                orderDetailMap.put(orderDetail.getItem().getItemId(), orderDetail);

                storingOrdersDetails();
            }

        }
        return "success";
    }


    private void prep() {
        List<Item> itemList = itemService.findAllWithDepth();
        Map session = ActionContext.getContext().getSession();
        if (itemList != null && itemList.size() > 0) {
            session.put("itemIdList", itemList);
        } else {
            session.put("itemIdList", "");
        }


    }

    private void retrievingOrdersDetails() {
        Map session = ActionContext.getContext().getSession();
        session = ActionContext.getContext().getSession();
        Object Obj = session.get("orderDetailMap");
        if (Obj != null) {

            orderDetailMap = (Map<Integer, OrderDetail>) Obj;
            orderDetails = orderDetailMap.values();
//            Iterator<OrderDetail> iterator =  orderDetailMap.values().iterator();
//            while (iterator.hasNext()){
//                orderDetails.add(iterator.next());
//            }

        }


    }
//
    private void storingOrdersDetails() {
        Map session = ActionContext.getContext().getSession();
        if (orderDetailMap != null && orderDetailMap.size() > 0) {
            session.put("orderDetailMap", orderDetailMap);
            orderDetails = orderDetailMap.values();
//            Iterator<OrderDetail> iterator =  orderDetailMap.values().iterator();
//                    while (iterator.hasNext()){
//                        orderDetails.add(iterator.next());
//                    }
        } else {
            session.put("orderDetailMap", null);
        }





    }


    public String delete() {
        Map session = ActionContext.getContext().getSession();
        session = ActionContext.getContext().getSession();
        Object Obj = session.get("orderDetailMap");
        if (Obj != null) {

            orderDetailMap = (Map<Integer, OrderDetail>) Obj;
            orderDetailMap.remove(orderDetail);
            session.put("orderDetailMap", orderDetailMap);
            orderDetails = orderDetailMap.values();
        }
        orderDetailMap.remove(orderDetail.getItem().getItemId());
        orderDetailMap.values();

        return SUCCESS;
    }


    private boolean validationSuccessful() {
        if (orderDetail.getQuantity() == null || orderDetail.getQuantity() <=0) {
            addActionMessage("Quantity is required");
        }
        if (orderDetail.getItem() == null || orderDetail.getItem().getItemId() ==-1) {
            addActionMessage("Item is required");
        }

        if (this.hasActionMessages()) {
            return false;
        } else {
            return true;
        }
    }


    public Collection<OrderDetail> getOrderDetails() {
        return orderDetails;
    }

    public void setOrderDetails(Collection<OrderDetail> orderDetails) {
        this.orderDetails = orderDetails;
    }

    public OrderDetail getOrderDetail() {
        return orderDetail;
    }

    public void setOrderDetail(OrderDetail orderDetail) {
        this.orderDetail = orderDetail;
    }
}
